# -*- coding: utf-8 -*-
"""
btc wallet bot Project

This project was developed by Seye (cescwalletbot@protonmail.com)

Dependencies:
pip pytelegrambotapi
pip pypng
pip pyqrcode
pip certifi
pip urllib3

"""


import json
import datetime
import urllib3
import certifi
import time
import os
import sqlite3


def is_int(s):
    """Check if a string can be converted to integer."""
    try:
        int(s)
        return True
    except ValueError:
        return False


def is_float(s):
    """Check if a string can be converted to float."""
    try:
        round(float(s), 8)
        return True
    except ValueError:
        return False


def cryptonator_request():
    """Makes price request to cryptonator."""
    headers = {"Accept": "application/json"}
    http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(), headers=headers)
    openlink_eur = http.request('GET', 'https://www.cryptonator.com/api/ticker/btc-eur')
    eur_data = json.loads(openlink_eur.data.decode())
    price_eur = float(eur_data['ticker']['price'])
    openlink_usd = http.request('GET', 'https://www.cryptonator.com/api/ticker/btc-usd')
    usd_data = json.loads(openlink_usd.data.decode())
    price_usd = float(usd_data['ticker']['price'])
    price = {'eur': round(price_eur, 8),
             'usd': round(price_usd, 8),
             'date': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
    with open('price.json', 'w') as f:
        json.dump(price, f)


def folder_structure():
    """Creates the initial folder structure."""
    os.mkdir('./log')
    os.mkdir('./log/userban')
    os.mkdir('./log/userlog')
    os.mkdir('./log/userpref')
    os.mkdir('./log/userqrcode')


def allowed_users(fulltext):
    """Create a file with a list of allowed users.
    
    Allows up to 100 users."""
    N=0
    allowed_users = [''] * 100
    print(fulltext['server_allowedusers_1'])
    print(fulltext['server_allowedusers_2'])
    allowed_users[N]=input(fulltext['server_allowedusers_3'] + str(N+1) + ': ')
    while len(allowed_users[N]) != 0:
        N = N+1
        allowed_users[N]=input(fulltext['server_allowedusers_3'] + str(N+1) + ': ')
    allowed_users = allowed_users[0:N]
    json.dump(allowed_users, open('allowed_users.cwb', 'w'))
    print(fulltext['server_allowedusers_4'])